package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.User;

import controller.WorkOutController;
import controller.WorkOutProgramController;

public class MyWorkOutProgramsTab extends JPanel {

	JTable table;
	JButton updateTable;
	
	public MyWorkOutProgramsTab(){
		
		updateTable = new JButton("Opdater Tabel");
		updateTable.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateWorkOutProgramsTable(User.getInstance().getUserId());
			}
		});
		
		
		ResultSet rs = null;
		WorkOutProgramController programCtr = WorkOutProgramController.getInstance();
		rs = programCtr.getUserWorkOutPrograms("");
		JPanel panel = new JPanel();
		// WorkOut Tabel   	
		table = new JTable();
		FillTable model = null;
		try {
			model = new FillTable(rs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		table.setModel(model);

		panel.add(table);

		JScrollPane jsp = new JScrollPane(table);
		add(jsp, BorderLayout.LINE_END);
		setVisible(true);
		add(panel,BorderLayout.LINE_END);
		panel.add(updateTable);
		//pack();
		setVisible(true);
	}
	public void updateWorkOutProgramsTable(String userId){
		ResultSet rs;
		WorkOutProgramController programCtr = WorkOutProgramController.getInstance();

		rs = programCtr.getUserWorkOutPrograms(userId);
		try {
			FillTable model = new FillTable(rs);
			table.setModel(model);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
